#include "QQFriendPage.h"

QQFriendPage::QQFriendPage(QWidget* parent)
	: ElaScrollPage(parent)
{
	m_friends = new ElaListView(this);
	m_friends_model = new QStandardItemModel(this);
	m_friends->setModel(m_friends_model);
	m_friends_delegate = new QQFriendIndexDelegate(this);
	m_friends->setItemDelegate(m_friends_delegate);
	m_friends->setItemHeight(80);
	m_friends->setUniformItemSizes(true);

	m_search = new ElaLineEdit(this);
	m_search->setPlaceholderText("Search");
	m_search->setFixedHeight(30);

	m_menu_button = new ElaIconButton(ElaIconType::Plus, this);
	m_menu_button->setFixedSize(30, 30);
	m_menu_button->setContentsMargins(0, 0, 0, 0);

	m_menu = new ElaMenu(this);
	m_menu->addAction("Add");
	m_menu->addAction("Remove");
	m_menu->hide();

	QHBoxLayout* menu_layout = new QHBoxLayout();
	menu_layout->addWidget(m_search);
	menu_layout->addWidget(m_menu_button);
	menu_layout->setContentsMargins(0, 0, 0, 0);
	menu_layout->setSpacing(0);

	m_notice_button = new ElaIconButton(ElaIconType::Bell, this);
	m_notice_button->setContentsMargins(0, 0, 0, 0);

	QVBoxLayout* friend_layout = new QVBoxLayout();
	friend_layout->addLayout(menu_layout);
	friend_layout->addWidget(m_notice_button);
	friend_layout->addWidget(m_friends);
	friend_layout->setContentsMargins(0, 0, 0, 0);
	friend_layout->setSpacing(5);

	m_friend_request = new ElaListView(this);
	m_friend_request_model = new QStandardItemModel(this);
	m_friend_request_delegate = new QQFriendRequestDelegate(this);
	m_friend_request->setItemDelegate(m_friend_request_delegate);
	m_friend_request->setModel(m_friend_request_model);
	m_friend_request->setEditTriggers(QAbstractItemView::NoEditTriggers);
	m_friend_request->setItemHeight(50);
	m_friend_request->setUniformItemSizes(true);

	m_tab = new ElaTabWidget(this);
	m_tab->tabBar()->hide();
	m_tab->addTab(m_friend_request, "");

	QHBoxLayout* layout = new QHBoxLayout();
	layout->addLayout(friend_layout);
	layout->addWidget(m_tab);
	layout->setContentsMargins(0, 0, 0, 0);
	layout->setSpacing(0);
	layout->setStretch(0, 1);
	layout->setStretch(1, 3);

	m_friend = new QWidget(this);
	m_friend->setLayout(layout);

	this->addCentralWidget(m_friend);
	this->setPageTitleSpacing(0);
	this->setTitleVisible(false);
	this->setContentsMargins(0, 0, 0, 0);

	connect(m_menu_button, &ElaIconButton::clicked, [this]() {m_menu->popup(QCursor::pos()); });
	connect(m_menu, &ElaMenu::triggered, this, &QQFriendPage::do_menu_action);
	connect(m_friend_request_delegate, &QQFriendRequestDelegate::add_friend_request_result, this, &QQFriendPage::do_add_friend_request);
}

QQFriendPage::~QQFriendPage()
{
}

void QQFriendPage::add_friend(const QQFriendIndexItem& _friend)
{
	QStandardItem* item = new QStandardItem;
	item->setData(_friend.account, Qt::UserRole + 0);
	item->setData(_friend.icon, Qt::UserRole + 1);
	item->setData(_friend.nickname, Qt::UserRole + 2);
	m_friends_model->appendRow(item);
}

void QQFriendPage::show_add_friend_dialog()
{

}

void QQFriendPage::add_add_friend_request_item(const QString& _friend)
{

}

void QQFriendPage::load_friends(const QList<QQFriendIndexItem>& _friends)
{
	int size = _friends.size();
	m_friends_model->insertRows(0, size);
	for (int i = 0; i < size; ++i)
	{
		QStandardItem* item = new QStandardItem;
		item->setData(_friends[i].account, Qt::UserRole + 0);
		item->setData(_friends[i].icon, Qt::UserRole + 1);
		item->setData(_friends[i].nickname, Qt::UserRole + 2);
		m_friends_model->setItem(i, item);
	}
}

void QQFriendPage::do_menu_action(QAction* action)
{
	if (action == m_menu->actions()[0])
	{
		show_add_friend_dialog();
	}
	else if (action == m_menu->actions()[1])
	{
		//emit remove_friend_click();
	}
}

void QQFriendPage::do_add_friend_request(const QModelIndex& index, int type)
{
	QString account = index.data(Qt::UserRole + 0).toString();
	emit add_friend_request_click(account, type);
}